<?php

namespace App\Admin\Controllers;

use App\BrokerBlockMobile;
use App\Http\Controllers\Controller;
use App\Service\TeleService;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BrokerBlockMobileController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('手机黑名单')
            ->description('手机黑名单')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('增加手机打入黑名单')
            ->description('')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BrokerBlockMobile);

        $grid->id('Id');
        $grid->mobile('手机号');
        $grid->created_at('创建时间');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BrokerBlockMobile::findOrFail($id));

        $show->id('Id');
        $show->mobile('Mobile');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BrokerBlockMobile);

        $form->mobile('mobile', '手机号')->rules('mobile|unique:broker_block_mobiles,mobile',[
            'mobile'=>'请输入手机号',
            'unique'=>'改手机号已经加入黑名单',
        ]);
        $form->saving(function($form){
           $tele= new TeleService();
           $tele->setSpBlockMobile($form->mobile);
        }

        );

        return $form;
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {

       $tele= new TeleService();

       $brokerBlock=BrokerBlockMobile::find($id);
       try {
           $tele->setSpBlockMobile($brokerBlock->mobile, 0, 1);
       }catch (\Exception $e){
           $data = [
               'status'  => false,
               'message' => $e->getMessage(),
           ];
       }
        if ($this->form()->destroy($id)) {
            $data = [
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ];
        } else {
            $data = [
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ];
        }

        return response()->json($data);
    }
}
